package com.ruoyi.hatha.controller;

import java.util.Calendar;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.hatha.domain.PCards;
import com.ruoyi.hatha.service.IPCardsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hatha.domain.PMyvip;
import com.ruoyi.hatha.service.IPMyvipService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我的会员卡Controller
 * 
 * @author Dong
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/hatha/myvip")
public class PMyvipController extends BaseController
{
    @Autowired
    private IPMyvipService pMyvipService;
    @Autowired
    private IPCardsService cardsService;

    /**
     * 查询我的会员卡列表
     */
    @PreAuthorize("@ss.hasPermi('hatha:myvip:list')")
    @GetMapping("/list")
    public TableDataInfo list(PMyvip pMyvip)
    {
        startPage();
        List<PMyvip> list = pMyvipService.selectPMyvipList(pMyvip);
        return getDataTable(list);
    }

    @GetMapping("/tingka")
    public AjaxResult tingka(Long id)
    {
        PMyvip myvip = pMyvipService.selectPMyvipById(id);
        myvip.setStatus(1);
        pMyvipService.updatePMyvip(myvip);
        return AjaxResult.success();
    }
    @GetMapping("/jianka")
    public AjaxResult jianka(Long id)
    {
        PMyvip myvip = pMyvipService.selectPMyvipById(id);
        myvip.setSycs(myvip.getSycs()-1);
        pMyvipService.updatePMyvip(myvip);
        return AjaxResult.success();
    }

    @GetMapping("/kaika")
    public AjaxResult kaika(Long id)
    {
        PMyvip myvip = pMyvipService.selectPMyvipById(id);
        myvip.setStatus(0);
        pMyvipService.updatePMyvip(myvip);
        return AjaxResult.success();
    }

    @GetMapping("/xufei")
    public AjaxResult xufei(Long id,Integer year)
    {
        PMyvip myvip = pMyvipService.selectPMyvipById(id);

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(myvip.getEndTime());

        calendar.add(Calendar.YEAR, year);

        myvip.setEndTime(calendar.getTime());

        pMyvipService.updatePMyvip(myvip);
        return AjaxResult.success();
    }


    @GetMapping("/zhuanka")
    public AjaxResult zhuanka(Long id,Long uid)
    {
        PMyvip myvip = pMyvipService.selectPMyvipById(id);
        myvip.setUid(uid);
        pMyvipService.updatePMyvip(myvip);
        return AjaxResult.success();
    }

    @GetMapping("/getMyvip")
    public AjaxResult getMyvip(){
        Long uid = SecurityUtils.getUserId();
        PMyvip mp = new PMyvip();
        mp.setUid(uid);
        List<PMyvip> lmp = pMyvipService.selectPMyvipList(mp);

        if(lmp.size()==0){
            return AjaxResult.error("未购买会员");
        }else{
            return AjaxResult.success(lmp.get(0));
        }
    }

    @GetMapping("/getuservip")
    public AjaxResult getuservip(Long uid){
        PMyvip mp = new PMyvip();
        mp.setUid(uid);
        List<PMyvip> lmp = pMyvipService.selectPMyvipList(mp);

        for(int i=0;i<lmp.size();i++){
            Long cid = lmp.get(i).getCardId();
            PCards cd = cardsService.selectPCardsById(cid);
            lmp.get(i).setCard(cd);
        }

        return AjaxResult.success(lmp);
    }

    /**
     * 导出我的会员卡列表
     */
    @PreAuthorize("@ss.hasPermi('hatha:myvip:export')")
    @Log(title = "我的会员卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PMyvip pMyvip)
    {
        List<PMyvip> list = pMyvipService.selectPMyvipList(pMyvip);
        ExcelUtil<PMyvip> util = new ExcelUtil<PMyvip>(PMyvip.class);
        util.exportExcel(response, list, "我的会员卡数据");
    }

    /**
     * 获取我的会员卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('hatha:myvip:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pMyvipService.selectPMyvipById(id));
    }

    /**
     * 新增我的会员卡
     */
    @PreAuthorize("@ss.hasPermi('hatha:myvip:add')")
    @Log(title = "我的会员卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PMyvip pMyvip)
    {
        return toAjax(pMyvipService.insertPMyvip(pMyvip));
    }

    /**
     * 修改我的会员卡
     */
    @PreAuthorize("@ss.hasPermi('hatha:myvip:edit')")
    @Log(title = "我的会员卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PMyvip pMyvip)
    {
        return toAjax(pMyvipService.updatePMyvip(pMyvip));
    }

    /**
     * 删除我的会员卡
     */
    @PreAuthorize("@ss.hasPermi('hatha:myvip:remove')")
    @Log(title = "我的会员卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pMyvipService.deletePMyvipByIds(ids));
    }
}
